package com.invokestatic.shooter.Screens;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.invokestatic.shooter.Art;
import com.invokestatic.shooter.Input;
import com.invokestatic.shooter.ShooterGame;

public abstract class Screen {

	private final String chars = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
	private ShooterGame shooterGame;
	public SpriteBatch spriteBatch;

	public final void init (ShooterGame shooterGame) {
		this.shooterGame = shooterGame;

		Matrix4 projection = new Matrix4();
		projection.setToOrtho(0, 1280, 720, 0, -1, 1);

		spriteBatch = new SpriteBatch(100);
		spriteBatch.setProjectionMatrix(projection);
	}

	protected void setScreen(Screen screen) {
		shooterGame.setScreen(screen);
	}

	public void draw(TextureRegion region, int x, int y) {
		int width = region.getRegionWidth();
		if(width < 0) width = -width;
		int height = region.getRegionHeight();
		if(height < 0) height = -height;
		
		spriteBatch.draw(region, x, y, width, height);
	}

	public void drawString (String string, int x, int y) {
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			draw(Art.font[yloc][xloc], x + (i * 12),y);
		}
	}
	
	public void drawStringRoatatedCentered(String string, int x, int y, int rotation) {
		double yRatio = Math.cos(Math.toRadians(rotation));
		double xRatio = Math.sin(Math.toRadians(rotation));
		x = x - (int)((yRatio * (string.length() * 12))/2);
		y = y - (int)((xRatio * (string.length() * 12))/2);
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			int xPos = (int) (x + (i * 12 * yRatio));
			int yPos = (int) (y + (i * 12 * xRatio));
			draw(Art.font[yloc][xloc], xPos, yPos, rotation, 1, 1);
		}
	}
	
	public void drawStringRoatatedCentered(String string, int x, int y, int rotation, float xScale, float yScale) {
		double yRatio = Math.cos(Math.toRadians(rotation));
		double xRatio = Math.sin(Math.toRadians(rotation));
		
		x = x - (int)((yRatio * (string.length() * 12))/2 * xScale);
		y = y - (int)((xRatio * (string.length() * 12))/2 * yScale);
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			int xPos = (int) (x + (i * xScale * 12 * yRatio));
			int yPos = (int) (y + (i * yScale * 12 * xRatio));
			draw(Art.font[yloc][xloc], xPos, yPos, rotation, xScale, yScale);
		}
	}
	
	public void drawStringRotated (String string, int x, int y, int rotation) {
		double yRatio = Math.cos(Math.toRadians(rotation));
		double xRatio = Math.sin(Math.toRadians(rotation));
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			int xPos = (int) (x + (i * 12 * yRatio));
			int yPos = (int) (y + (i * 12 * xRatio));
			draw(Art.font[yloc][xloc], xPos, yPos, rotation, 1, 1);
		}
	}
	
	public void drawStringRotated (String string, int x, int y, int rotation, float xScale, float yScale) {
		double yRatio = Math.cos(Math.toRadians(rotation));
		double xRatio = Math.sin(Math.toRadians(rotation));
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			int xPos = (int) (x + (i * xScale * 12 * yRatio));
			int yPos = (int) (y + (i * yScale * 12 * xRatio));
			draw(Art.font[yloc][xloc], xPos, yPos, rotation, xScale, yScale);
		}
	}
	
	
	
	public void draw(TextureRegion textureRegion, int x, int y, float rotation, float xScale, float yScale) {
//		spriteBatch.draw(textureRegion, x, y, x, y, textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), rotation, true);
		spriteBatch.draw(textureRegion, x, y, textureRegion.getRegionWidth()/2, textureRegion.getRegionHeight()/2, textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), 1 * xScale, 1 * yScale, rotation);
	}

	public void drawStringCentered(String string, int x, int y) {
		int length = string.length() * 12;
		x -= length/2;
		for (int i = 0; i < string.length(); i++) {
			char ch = string.charAt(i);
			int xloc = chars.indexOf(ch+"") / 16;
			int yloc = chars.indexOf(ch+"") % 16;
			draw(Art.font[yloc][xloc], x + (i * 12),(y-8));
		}
	}

	public void removed() {
		spriteBatch.dispose();
	}

	public abstract void render();

	public abstract void tick(Input input);

	public void drawRotated(TextureRegion textureRegion, int x, int y, int rotation, float xScale, float yScale) {
		spriteBatch.draw(textureRegion, x, y, textureRegion.getRegionWidth()/2, textureRegion.getRegionHeight()/2, textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), 1 * xScale, 1 * yScale, rotation);
	}

	public void draw(TextureRegion region, int x, int y, int sx, int sy) {
		int width = region.getRegionWidth();
		if(width < 0) width = -width;
		int height = region.getRegionHeight();
		if(height < 0) height = -height;
		
		spriteBatch.draw(region, x, y, width*sx, height*sy);
//		spriteBatch.draw
	}

}
